package com.huarui.user.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ewaf.framework.exception.BizException;
import org.ewaf.framework.model.IDataObject;
import org.ewaf.framework.service.BaseService;
import org.ewaf.framework.util.MD5Digester;
import com.huarui.user.dao.IUserDao;
import com.huarui.user.error.UserError;
import com.huarui.user.model.User;
/**
 * xzz
 * @author Administrator
 *
 */
public class UserService extends BaseService {

	private IUserDao getDao() {
		return (IUserDao) dao;
	}

	private String activeEmailSubjectTemplate;

	public void setActiveEmailSubjectTemplate(String activeEmailSubjectTemplate) {
		this.activeEmailSubjectTemplate = activeEmailSubjectTemplate;
	}

	private String activeEmailContentTemplate;

	public void setActiveEmailContentTemplate(String activeEmailContentTemplate) {
		this.activeEmailContentTemplate = activeEmailContentTemplate;
	}

	private String passwordEmailSubjectTemplate;

	public void setPasswordEmailSubjectTemplate(
			String passwordEmailSubjectTemplate) {
		this.passwordEmailSubjectTemplate = passwordEmailSubjectTemplate;
	}

	private String passwordEmailContentTemplate;

	public void setPasswordEmailContentTemplate(
			String passwordEmailContentTemplate) {
		this.passwordEmailContentTemplate = passwordEmailContentTemplate;
	}

	/*
	 * private TaskQueue taskQueue;
	 * 
	 * public void setTaskQueue(TaskQueue taskQueue) { this.taskQueue =
	 * taskQueue; }
	 */

	private SendEmail sendEmail;

	public SendEmail getSendEmail() {
		return sendEmail;
	}

	public void setSendEmail(SendEmail sendEmail) {
		this.sendEmail = sendEmail;
	}

	/**
	 * 调用设置激活码信息
	 * 
	 * @param activeCode
	 */

	public void setActiveCode(String activeCode) {

		sendEmail.setActiveCode(activeCode);

	}

	/**
	 * use for SendEmail
	 * 
	 * @param user
	 */
	public void setUserName(String userName) {

		sendEmail.setUserName(userName);

	}

	public void setEmail(String email) {

		sendEmail.setEmail(email);

	}

	/**
	 * 根据用户名查询用户信息
	 * 
	 * @param name
	 * @return
	 */
	public List<User> getByName(String name) {
		return getDao().getUserByName(name);
	}

	/**
	 * 根据邮件地址查询用户信息
	 * 
	 * @param email
	 * @return
	 */
	public User getUserByEmail(String email) {
		return getDao().getUserByEmail(email);
	}

	@Override
	public void create(IDataObject data) {

		User user = (User) data;

		super.create(user);
	}

	/**
	 * 发送激活邮件
	 */
	public void sendActiveEmail(User user) {
		Map<String, String> data = new HashMap<String, String>();

		data.put("charset", "UTF-8");
		data.put("recipients", user.getEmail());
		data.put("subjectTemplate", activeEmailSubjectTemplate);
		data.put("contentTemplate", activeEmailContentTemplate);
		data.put("userName", user.getName());
		data.put("activeCode", user.getActivateCode());
		sendEmail.send(data);
		/*
		 * Task task = new Task(); task.setType("commonEmail");
		 * task.setId(UUID.randomUUID().toString()); task.setExecTime(8);
		 * task.setDataMap(data); taskQueue.addTask(task);
		 */
	}

	/**
	 * 
	 * 激活用户
	 */
	public void activeUser(User user) {

		if (user == null) {

			throw new BizException(UserError.USER_NOT_EXSIT);

		}

		if (User.STATUS_ACTIVED.equals(user.getStatus())) {

			throw new BizException(UserError.ALREADY_BE_ACTIVED);

		}

		user.setActivateCode(null);

		user.setStatus(User.STATUS_ACTIVED);

		getDao().update(user);
	}

	/**
	 * 检验登入时的用户名和密码， 若检验成功，返回用户信息
	 */
	public User validateUser(String email, String password) throws Exception {
		User user = getUserByEmail(email);
		if (user == null) {
			throw new RuntimeException(UserError.WRONG_NAME_OR_EMAIL.getChError());
		}
		
		String pwd = MD5Digester.digest(password);

		if (!pwd.equals(user.getPassword())) {
			throw new RuntimeException(UserError.WRONG_NAME_OR_EMAIL.getChError());
		}

		return user;
	}

	/**
	 * 修改密码
	 */
	public void changePassword(Integer id, String password) {
		getDao().updateUserPassword(id, MD5Digester.digest(password));
	}

	/**
	 * 根据月份统计注册人数
	 */
	@SuppressWarnings("unchecked")
	public List<Map> countByCreated() {
		return getDao().countByCreated();
	}

	/**
	 * 根据月份获得email列表
	 */
	public List<String> getEmailsByCreated(List<String> ymCreated) {
		return getDao().getEmailsByCreated(ymCreated);
	}

	/**
	 * email拒绝接收营销邮件
	 */
	public void rejectEmail(String email) {
		getDao().rejectEmail(email);
	}

	public void sendPasswordEmail(User user) {

		try {
			this.getDao().update(user);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		Map<String, String> data = new HashMap<String, String>();
		data.put("charset", "UTF-8");
		data.put("activeCode", user.getActiveCode());
		data.put("recipients", user.getEmail());
		data.put("subjectTemplate", passwordEmailSubjectTemplate);
		data.put("contentTemplate", passwordEmailContentTemplate);

		sendEmail.send(data);
	}

	/**
	 * 发送密码重置邮件
	 */
	public void sendPasswordEmail(User user, String password) {
		Map<String, String> data = new HashMap<String, String>();

		data.put("charset", "UTF-8");
		data.put("recipients", user.getEmail());
		data.put("subjectTemplate", passwordEmailSubjectTemplate);
		data.put("contentTemplate", passwordEmailContentTemplate);
		data.put("userName", user.getName());
		data.put("password", password);

		sendEmail.send(data);
		/*
		 * Task task = new Task(); task.setType("commonEmail");
		 * task.setId(UUID.randomUUID().toString()); task.setExecTime(0);
		 * task.setDataMap(data);
		 * 
		 * taskQueue.addTask(task);
		 */
	}

	public List<User> getByPyCode(String pyCode) {
		return getDao().getUserByPyCode(pyCode);
	}

	public User getByActiveCode(String activeCode) {

		return this.getDao().getByActiveCode(activeCode);

	}

	public User getUserByIdent(String ident) {

		return this.getDao().getUserByIdent(ident);

	}

	public Integer getUserCountByConditions(Map<String, Object> map) {
		
		return getDao().getUserCountByConditions(map);
	}

	@SuppressWarnings("unchecked")
	public List<Map> getAllUserByConditions(Map<String, Object> map) {
		
		return getDao().getUser4MapByConditions(map);
	}
	
	public User getUserByActiveCode(String activeCode){
		
		return getDao().getUserByActiveCode(activeCode);
	}

}
